package com.meihua.shellapp.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aspsine.irecyclerview.IViewHolder;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.ClassRoomDetailActivity;
import com.meihua.shellapp.activity.ClassroomKnowledgeActivity;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.bean.ClassroomListBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.dialog.LoginandRegistrationDialog;
import com.meihua.shellapp.utils.DateUtils;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

import java.util.List;

/**
 * Created by ${zhangyuanchao} on 2017/10/23.
 */

public class ClassroomKnowledgeAdapter extends RecyclerView.Adapter<IViewHolder> {

    private List<ClassroomListBean.DataBean> mData;
    private ClassroomKnowledgeActivity mActivity;
    private LoginandRegistrationDialog dialog;

    public ClassroomKnowledgeAdapter(List<ClassroomListBean.DataBean> data, ClassroomKnowledgeActivity activity) {
        this.mData = data;
        this.mActivity = activity;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnItemClickListener mOnItemClickListener;

    @Override
    public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_release, parent, false));
    }

    @Override
    public void onBindViewHolder(IViewHolder holder, int position) {

        ((ViewHolder) holder).setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ViewHolder extends IViewHolder {
        TextView tvArticleTitle;
        TextView tvArticleContent;
        TextView tvReleaseTime;
        TextView tveRleaseDel;

        public ViewHolder(View itemView) {
            super(itemView);
            tvArticleTitle = itemView.findViewById(R.id.tv_article_title);
            tvArticleContent = itemView.findViewById(R.id.tv_article_content);
            tvReleaseTime = itemView.findViewById(R.id.tv_release_time);
            tveRleaseDel = itemView.findViewById(R.id.tv_release_del);
            tveRleaseDel.setVisibility(View.GONE);
        }

        /*设置数据*/
        public void setData(final ClassroomListBean.DataBean dataBean) {
            tvArticleTitle.setText(dataBean.getTitle());
            tvArticleContent.setText(dataBean.getContent());

            tvReleaseTime.setText(DateUtils.getInstance().getTime(dataBean.create_time));
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!SharedPreferencesUtil.getInstance().getBoolean(mActivity, Constant.ISLOGIN, false)) {
                        dialog = new LoginandRegistrationDialog.Builder(mActivity)
                                .setClickSure(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent(mActivity, LoginActivity.class);
                                        mActivity.startActivity(intent);
                                        dialog.dismiss();
                                    }
                                })
                                .setClickCencle(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mActivity.startActivity(new Intent(mActivity, WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                                        dialog.dismiss();
                                    }
                                })
                                .setMessage("请您登录注册！")
                                .show();
                        return;
                    }
                    Intent intent = new Intent(mActivity, ClassRoomDetailActivity.class);
                    intent.putExtra("id", dataBean.getId());
                    intent.putExtra("title", dataBean.getTitle());
                    intent.putExtra("study_url", dataBean.getStudy_url());//视频URL
                    intent.putExtra("attachment_type", dataBean.getAttachment_type());//发布类型：1=>视频，3=>文章
                    mActivity.startActivity(intent);
                }
            });
//            dataBean.getAttachment_type()//发布类型：1=>视频，3=>文章
//            study_url	string	视频URL
        }
    }

}
